﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
namespace SQT.tracnghiem.Ajax.ASP.Class
{
    public partial class check_in_user : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int cls = int.Parse(Request.QueryString["cls"]);
            string check = Request.QueryString["check"];
            int shift = int.Parse(Request.QueryString["shift"]);
            DateTime date = DateTime.Parse(Request.QueryString["date"]);
            string[] split = new string[check.Split(',').Length];
            split = check.Split(',');
            string[] split2 = new string[2];
            for (int i = 0; i < check.Split(',').Length - 1; i++)
            {
                split2 = split[i].Split('_');
                var check_user = new TblCheckUserController().FetchAll().Where(x => x.UserId == int.Parse(split2[0]) && DateTime.Parse(x.DateX.ToString()).Date == date.Date && x.Shifts == shift).ToList();
                if (check_user.Count != 0)
                {
                    if (int.Parse(split2[1]) == 1)
                        new TblCheckUserController().Update(check_user[0].Id, check_user[0].UserId, check_user[0].ClassX, check_user[0].DateX, check_user[0].Shifts, true, false, false, false);
                    else
                        if (int.Parse(split2[1]) == 2)
                            new TblCheckUserController().Update(check_user[0].Id, check_user[0].UserId, check_user[0].ClassX, check_user[0].DateX, check_user[0].Shifts, false, true, false, false);
                        else
                            if (int.Parse(split2[1]) == 3)
                                new TblCheckUserController().Update(check_user[0].Id, check_user[0].UserId, check_user[0].ClassX, check_user[0].DateX, check_user[0].Shifts, false, false, true, false);
                            else
                                new TblCheckUserController().Update(check_user[0].Id, check_user[0].UserId, check_user[0].ClassX, check_user[0].DateX, check_user[0].Shifts, false, false, false, true);
                }
                else
                {
                    if (split2[1] == "")
                        new TblCheckUserController().Insert(int.Parse(split2[0]), cls, date, shift, false, false, false, true);
                    else
                        if (int.Parse(split2[1]) == 1)
                            new TblCheckUserController().Insert(int.Parse(split2[0]), cls, date, shift, true, false, false, false);
                        else
                            if (int.Parse(split2[1]) == 2)
                                new TblCheckUserController().Insert(int.Parse(split2[0]), cls, date, shift, false, true, false, false);
                            else
                                if (int.Parse(split2[1]) == 3)
                                    new TblCheckUserController().Insert(int.Parse(split2[0]), cls, date, shift, false, false, true, false);
                                else
                                    new TblCheckUserController().Insert(int.Parse(split2[0]), cls, date, shift, false, false, false, true);


                }
            }
        }
    }
}